package com.simple.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.simple.common.PageResult;
import com.simple.dto.BillDTO;
import com.simple.mapper.BillMapper;
import com.simple.pojo.Bill;
import com.simple.service.BillService;
import com.simple.utils.BeanConverterUtil;
import com.simple.utils.TimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName BillServiceImpl
 * @Author SimpleTree
 **/
@Service
public class BillServiceImpl implements BillService {

    @Resource
    private BillMapper billMapper;
    @Autowired
    private BeanConverterUtil beanConverterUtil;

    @Override
    public PageResult getBills(BillDTO billDTO) {
        PageHelper.startPage(billDTO.getPageNum(), billDTO.getPageSize(),"cost_time desc");

        Example example = new Example(Bill.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("userId", billDTO.getUserId());
        if (billDTO.getStartTime() != null) {
            criteria.andGreaterThan("costTime", billDTO.getStartTime());
        }
        if (billDTO.getEndTime() != null) {
            criteria.andLessThan("costTime", billDTO.getEndTime());
        }
        List<Bill> bills = billMapper.selectByExample(example);

        PageInfo<Bill> pageInfo = new PageInfo<>(bills);
        return new PageResult(pageInfo.getTotal(), pageInfo.getPageNum(), beanConverterUtil.toBillDTO(bills));
    }

    @Override
    public BillDTO getBillById(Integer id) {
        Bill bill = billMapper.selectByPrimaryKey(id);
        BillDTO billDTO = beanConverterUtil.toBillDTO(bill);
        return billDTO;
    }

    @Override
    public void saveBill(BillDTO billDTO) {
        Bill bill = beanConverterUtil.toBill(billDTO);
        bill.setCreateTime(new Date());
        bill.setUpdateTime(new Date());
        billMapper.insert(bill);
    }

    @Override
    public void updateBill(BillDTO billDTO) {
        billMapper.updateByPrimaryKeySelective(beanConverterUtil.toBill(billDTO));
    }

    @Override
    public void deleteByIds(List<Integer> ids) {
        billMapper.deleteByIds(ids);
    }

    /**
     * 获取本月所有消费
     *
     * @return
     */
    @Override
    public Double getMonthBillMoney(Integer userId) {
        Map<String, Object> params = new HashMap<>();
        List<Bill> bills = getBillByCostTime(params,userId);

        BigDecimal totalBillMoney = new BigDecimal("0");
        for (Bill bill : bills) {
            totalBillMoney = totalBillMoney.add(new BigDecimal(bill.getMoney() + ""));
        }

        return totalBillMoney.doubleValue();
    }

    @Override
    public Double getMoneyOfMonthByBillType(Integer billTypeCode,Integer userId) {
        Map<String, Object> params = new HashMap<>();
        params.put("billType", billTypeCode);
        List<Bill> bills = getBillByCostTime(params,userId);

        BigDecimal res = new BigDecimal("0");
        for (Bill bill : bills) {
            res = res.add(new BigDecimal(bill.getMoney() + ""));
        }
        return res.doubleValue();
    }

    public List<Bill> getBillByCostTime(Map<String,Object> params,Integer userId) {
        params.put("userId", userId);
        Date firstDayOfMonth = TimeUtil.getFirstDayOfMonth();
        Date lastDayOfMonth = TimeUtil.getLastDayOfMonth();

        Example example = new Example(Bill.class);
        Example.Criteria criteria = example.createCriteria();

        criteria.andGreaterThan("costTime", firstDayOfMonth);
        criteria.andLessThan("costTime", lastDayOfMonth);
        if (!CollUtil.isEmpty(params)) {
            for (Map.Entry<String, Object> entry : params.entrySet()) {
                criteria.andEqualTo(entry.getKey(), entry.getValue());
            }
        }
        return billMapper.selectByExample(example);
    }
}
